{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "2d7486a8",
   "metadata": {},
   "source": [
    "# hvplot.plotting.lag_plot\n",
    "\n",
    "```{eval-rst}\n",
    ".. currentmodule:: hvplot.plotting\n",
    "\n",
    ".. autofunction:: lag_plot\n",
    "```\n",
    "\n",
    "## Examples\n",
    "\n",
    "### Simple lag plot\n",
    "\n",
    "Lag plots are most commonly used to look for patterns in time series data. This example shows a lag plot constructed from a time series. The lag plot suggests that the time series exhibits positive autocorrelation at a lag of 1 (the default setting)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a9b61fca-fb14-4841-b896-33fc8c4b966a",
   "metadata": {},
   "outputs": [],
   "source": [
    "import hvplot.pandas  # noqa\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "\n",
    "np.random.seed(42)\n",
    "series = pd.Series(np.cumsum(np.random.randn(100)))\n",
    "\n",
    "series.hvplot.line(title=\"Time series\", frame_width=300) +\\\n",
    "hvplot.plotting.lag_plot(series, title=\"Lag plot\", frame_width=300)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "96bd1c24-643a-474a-84c3-e1b3ab1ac59e",
   "metadata": {},
   "source": [
    "### With multiple variables\n",
    "\n",
    "In this example, we use a lag of 52 to compare annual patterns, as the dataset contains weekly data."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "85d93412-bcc1-4571-a0c7-0cedd33e1892",
   "metadata": {},
   "outputs": [],
   "source": [
    "import hvplot\n",
    "\n",
    "df = hvplot.sampledata.stocks(\"pandas\").set_index(\"date\")\n",
    "\n",
    "hvplot.plotting.lag_plot(\n",
    "    df.loc[\"2020\":], lag=52, data_aspect=1,\n",
    "    xlim=(0, 6), ylim=(0, 6)\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8c3c48f3-ba12-4d3d-a93e-b993f3631d9a",
   "metadata": {},
   "source": [
    "### Use of `lag`\n",
    "\n",
    "For this dataset built from a random walk, the autocorrelation is maximum with a lag of `1`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a39c97f5-52cb-4232-ba68-19a86c023a8b",
   "metadata": {},
   "outputs": [],
   "source": [
    "import hvplot\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "\n",
    "np.random.seed(42)\n",
    "series = pd.Series(np.cumsum(np.random.randn(300)))\n",
    "\n",
    "plot_opts = dict(frame_width=200)\n",
    "hvplot.plotting.lag_plot(series, lag=1, title=\"lag=1 (default)\", **plot_opts) +\\\n",
    "hvplot.plotting.lag_plot(series, lag=5, title=\"lag=5\", **plot_opts) +\\\n",
    "hvplot.plotting.lag_plot(series, lag=100, title=\"lag=50\", **plot_opts)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7bef31ca-cdfd-490d-9120-83920ac67830",
   "metadata": {},
   "source": [
    "### Different kinds of autocorrelation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ae3f8a76-f8c3-4202-8dea-a903f4a1c880",
   "metadata": {},
   "outputs": [],
   "source": [
    "import hvplot\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "\n",
    "np.random.seed(42)\n",
    "df = pd.DataFrame({\n",
    "    \"positive\": np.cumsum(np.random.randn(100)),\n",
    "    \"noise\": np.random.randn(100),\n",
    "    \"cyclic\": np.sin(np.linspace(0, 4 * np.pi, 100))\n",
    "})\n",
    "\n",
    "plot_opts = dict(frame_width=200, shared_axes=False)\n",
    "hvplot.plotting.lag_plot(df[\"noise\"], title=\"No autocorrelation\", **plot_opts) +\\\n",
    "hvplot.plotting.lag_plot(df[\"positive\"], title=\"Positive autocorrelation\", **plot_opts) +\\\n",
    "hvplot.plotting.lag_plot(df[\"cyclic\"], title=\"Cyclic autocorrelation\", **plot_opts)"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python",
   "pygments_lexer": "ipython3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
